package com.wc.alorithm_luogu.P1115;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;

/**
 * @Author congege
 * @Description 最大子段和
 * https://www.luogu.com.cn/problem/P1115
 * @Date Created in 2023/9/29-21:02
 */
public class Main {
    public static void main(String[] args) {
        Input scan = new Input();
        int n = scan.nextInt();

        int[] array = new int[n];

        for (int i = 0; i < n; i++) {
            array[i] = scan.nextInt();
        }

        /**
         * 存储合并过后的数组
         */
        LinkedList<Integer> combineSubArray = new LinkedList<>();

        int curSum = array[0];
        /**
         * 判断前一个数是正的还是负数
         */
        boolean preNegative = array[0] < 0;
        int maxSubArraySum = curSum;
        /**
         * 将相邻的正数负全部合并
         * 将相邻的负数全部合并
         *
         *
         */
        for (int i = 1; i < n; i++) {
            if (preNegative) {
                if (array[i] < 0) {
                    maxSubArraySum = Math.max(maxSubArraySum, array[i]);
                    curSum += array[i];
                } else {
                    combineSubArray.add(curSum);
                    curSum = array[i];
                    preNegative = false;
                }
            } else {
                if (array[i] >= 0) {
                    curSum += array[i];
                    maxSubArraySum = Math.max(maxSubArraySum, curSum);
                } else {
                    combineSubArray.add(curSum);
                    curSum = array[i];
                    preNegative = true;
                }
            }
        }
        combineSubArray.add(curSum);

        /**
         * 首相为负的直接去除
         * 保证后续是 + - + - 的交错进行
         * 这样才能保证更大的子列和
         */
        if (combineSubArray.get(0) < 0) {
            combineSubArray.removeFirst();
        }

        /**
         * 能叠加的合并
         */
        if (combineSubArray.size() <= 2) {
            System.out.println(maxSubArraySum);
            return;
        } else {
            while (combineSubArray.size() > 2) {
                int left = combineSubArray.get(0);
                /**
                 * 这里mid 取负了
                 */
                int mid = -combineSubArray.get(1);
                int right = combineSubArray.get(2);

                /**
                 * 无论下面有没有都会删除
                 * 保证剩下的数为  + - + - 交错叠加
                 */
                combineSubArray.removeFirst();
                combineSubArray.removeFirst();
                /**
                 * 如果mid < left 时候 他是能加强right的就保留形成更大的正数
                 * 使得right = left + right - mid
                 */
                if (mid < left) {
                    int num = left + right - mid;
                    combineSubArray.set(0, num);
                    maxSubArraySum = Math.max(maxSubArraySum, num);
                }
            }
        }
        if (combineSubArray.size()>0){
            maxSubArraySum = Math.max(maxSubArraySum, combineSubArray.get(0));
        }
        System.out.println(maxSubArraySum);
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
